package com.fyb.springcloud.service.impl;

import com.fyb.springcloud.service.CacheService;
import com.netflix.discovery.converters.Auto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @author fyb
 * @version 1.0
 * @date 2021/3/11 16:26
 */
@Service("CacheService")
public class CacheServiceImpl implements CacheService {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public boolean save2Redis(String key, String value, long expireTime) {
        // expireTime =0 -> -1
        // expireTime =-2 -> 2
        // expireTime =2 -> 2
        if (expireTime == 0) {
            expireTime = -1;
        } else if (expireTime < -1) {
            expireTime = Math.abs(expireTime);
        }
        redisTemplate.opsForValue().set(key, value, expireTime, TimeUnit.MINUTES);

        return true;
    }

    @Override
    public String getFromRedis(String key) {
        Object o = redisTemplate.opsForValue().get(key);
        return o == null ? null : o.toString();
    }

    @Override
    public boolean deleteKey(String key) throws Exception {
        Boolean delete = redisTemplate.delete(key);
        return delete;
    }

    @Override
    public boolean expire(String key, long expireTime) {
        Boolean expire = redisTemplate.expire(key, expireTime, TimeUnit.MINUTES);
        return expire;
    }

    @Override
    public Long getAutoIncrementId(String key) {
        // 进行自增 数据，
        return redisTemplate.opsForValue().increment(key);
    }

    @Override
    public Set<Object> sMembers(String key) {
        Set<Object> difference = redisTemplate.opsForSet().members(key);
        return difference;
    }

    @Override
    public Long sAdd(String key, String value) {
        //向set中添加一条数据
        return redisTemplate.opsForSet().add(key,value);
    }

    @Override
    public Long sAdd(String key, String[] values) {
        //向set中批量添加数据
        return redisTemplate.opsForSet().add(key, values);
    }

    @Override
    public Long sRemove(String key, String value) {
        //向set中删除指定的key 数据
        Long remove = redisTemplate.opsForSet().remove(key, value);
        return remove;
    }

    @Override
    public void hSet(String key, String filed, String value) {
        redisTemplate.opsForHash().put(key, filed, value);
    }

    @Override
    public String hGet(String key, String filed) {
        Object o = redisTemplate.opsForHash().get(key, filed);
        return o == null ? null : o.toString();
    }

    @Override
    public Map<Object, Object> hGetAll(String key) {
        Map<Object, Object> entries = redisTemplate.opsForHash().entries(key);
        return entries;
    }

    @Override
    public boolean setNX(String key, String value, long expireSecond) {
        if (expireSecond == 0) { //按我们自己的要求，如果是0，那就设置为-1
            expireSecond = -1;
        } else if (expireSecond < -1) {
            expireSecond = Math.abs(expireSecond);//小于-1，则取绝对值
        }

        Boolean aBoolean = redisTemplate.opsForValue().setIfAbsent(key, value, expireSecond, TimeUnit.MINUTES);
        return aBoolean;
    }

    @Override
    public boolean hMset(String key, Map<String, Object> map) {
        redisTemplate.opsForHash().putAll(key, map);
        return true;
    }

    @Override
    public  Set<String> selectByPattern(String pattern) {
        return redisTemplate.keys(pattern);
    }

    @Override
    public Long hIncrement(String key, String field, long delta) {
        return redisTemplate.opsForHash().increment(key, field, delta);
    }
}
